package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hm
 * @package com.sky.service.impl
 * @date Created in 2024/11/5 09:23
 */
@Service
public class ShoppingCartImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        //拷贝属性
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询数据库中是否有该商品
         ShoppingCart cart= shoppingCartMapper.selectBy(shoppingCart);
        if (cart != null) {
            //如果有将商品的数量加1
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }else { //如果没有，将商品添加进数据库
            //判断dish_id是否为null，如果不为空，添加菜品信息
            if (dto.getDishId() != null){
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else {
                //否则添加套餐信息
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            //不论是菜品还是套餐 都有公共的  数量一开始为1  创建时间  获得userId
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //将购物车插入数据库中
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     */
    public List<ShoppingCart> showShoppingCart() {
        List<ShoppingCart> list = shoppingCartMapper.list(BaseContext.getCurrentId());
        return list;
    }
    /**
     * 清空购物车
     */
    public void clean() {
        // 注意：只能清空自己名下的购物车
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    @Override
    /**
     * 根据用户id在数据库中进行查询
     * 查询到 判断数量是否是1，为1直接删除，不为1更新数据中的数量（减一）
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectBy(shoppingCart);
        if (shoppingCart1.getNumber() > 1){
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartMapper.update(shoppingCart1);
        }else {
            shoppingCartMapper.deleteById(shoppingCart1.getId());
        }
    }
}
